package com.lap.framework.generator.configure;

import com.lap.framework.common.tool.JsonUtil;
import com.lap.framework.generator.shared.exception.ConfigError;
import java.io.IOException;
import java.io.InputStream;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.yaml.snakeyaml.Yaml;

@Slf4j
public class ConfigLoader {

  public static EnvConfig load() {
    Yaml yaml = new Yaml();
    try {
      ClassPathResource resource = new ClassPathResource("config.yaml");
      try (InputStream inputStream = resource.getInputStream()) {
        EnvConfig config = yaml.loadAs(inputStream, EnvConfig.class);
        log.info("loader yaml :{}", JsonUtil.pretty(config));
        return config;
      }
    } catch (IOException e) {
      throw new ConfigError("loader yaml error:", e);
    }
  }
}
